Shader "Reflective/Ligtmapped Specular" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
	_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
	_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
	_MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
	_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
	_LightMap ("Lightmap (RGB)", 2D) = "black" {}
}

Category {
	Blend AppSrcAdd AppDstAdd
	Fog { Color [_AddFog] }
	
	// ------------------------------------------------------------------
	// ARB fragment program / Radeon 9000
	
	SubShader {
		UsePass "Reflective/VertexLit/BASE" 
		Pass { 
			Name "BASE"
			Tags {"LightMode" = "Vertex"}
			Blend AppSrcAdd AppDstAdd
			Material {
				Diffuse [_Color]
				Shininess [_Shininess]
				Specular [_SpecColor]
			}
			Lighting On
			SeparateSpecular on
			SetTexture [_MainTex] { combine texture alpha * primary DOUBLE, texture * primary }
		}
		//UsePass "Bumped Specular/PPL"
		
		Pass {
			Tags { "LightMode" = "Always" }
			blend one one
			Color [_PPLAmbient]
			BindChannels {
				Bind "Vertex", vertex
				Bind "normal", normal
				Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
				Bind "texcoord", texcoord1 // main uses 1st uv
			}
			SetTexture [_LightMap] {
				constantColor [_Color]
				combine texture * constant
			}
			SetTexture [_MainTex] {
				constantColor [_Color]
				combine texture * previous, texture * constant
			}
		}

		Pass {

			Name "PPL"	
			Tags { "LightMode" = "Pixel" }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_builtin
			#pragma fragmentoption ARB_fog_exp2
			#pragma fragmentoption ARB_precision_hint_fastest 
			#include "UnityCG.cginc"
			#include "AutoLight.cginc" 

			struct v2f {
				V2F_POS_FOG;
				LIGHTING_COORDS
				float3	uvK : TEXCOORD0; // xy = UV, z = specular K
				float3	normal : TEXCOORD1;
				float2	uv3 : TEXCOORD2;
				float3	viewDirT;
				float3	lightDirT;
			}; 

			struct appdata_tanX {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;
				float4 texcoord1 : TEXCOORD1;
			};
			uniform float4 _MainTex_ST, _LightMap_ST, _Color;
			uniform float _Shininess;


			v2f vert (appdata_tanX v)
			{	
				v2f o;
				PositionFog( v.vertex, o.pos, o.fog );
				o.uvK.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.uvK.z = _Shininess * 128;
				o.uv3 = TRANSFORM_TEX(v.texcoord1, _LightMap);
				o.normal = v.normal;
				
				o.lightDirT = ObjSpaceLightDir( v.vertex );	
				o.viewDirT = ObjSpaceViewDir( v.vertex );	

				TRANSFER_VERTEX_TO_FRAGMENT(o);	
				return o;
			}

			uniform sampler2D _MainTex;
			uniform sampler2D _LightMap;
			
			float4 frag (v2f i) : COLOR
			{		
				float4 texcol = tex2D( _MainTex, i.uvK.xy );
				float4 texlmap = tex2D( _LightMap, i.uv3.xy );
				
				// get normal from the normal map
				
				half4 c = SpecularLight( i.lightDirT, i.viewDirT, normalize(i.normal), texcol, i.uvK.z, LIGHT_ATTENUATION(i) ) + texcol*texlmap*_Color;
				return c;
			}
			ENDCG  
		}
	}
}

FallBack "Reflective/VertexLit", 1

}